package com.yoso.shop.infrastructure.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * 日期工具类
 */
public class DateUtil {
    public static final String DATE_FORMAT_PATTERN = "yyyy-MM-dd";
    public static final String DATE_TIME_FORMAT_PATTERN = "yyyy-MM-dd";

    /**
     * date2比date1多的天数
     */
    public static int dateDiff(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        int day1 = cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        //同一年
        if (year1 != year2) {
            int timeDistance = 0;
            for (int i = year1; i < year2; i++) {
                //闰年
                if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) {
                    timeDistance += 366;
                } else {
                    //不是闰年
                    timeDistance += 365;
                }
            }

            return timeDistance + (day2 - day1);
        } else {
            //不同年
            return day2 - day1;
        }
    }

    /**
     * 获取当前日期（2021-10-11 00:00:00）
     */
    public static Date getToday() {
        Calendar calendar = Calendar.getInstance();
        //这是将当天的【秒】设置为0
        calendar.set(Calendar.SECOND, 0);
        //这是将当天的【分】设置为0
        calendar.set(Calendar.MINUTE, 0);
        //这是将当天的【时】设置为0
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        //2021-02-24 00:00:00
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_TIME_FORMAT_PATTERN);
        String ymd = sdf.format(calendar.getTime());

        try {
            return sdf.parse(ymd);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 格式化日期 到天
     */
    public static Date formatDate(String date) {
        return format(date, DATE_FORMAT_PATTERN);
    }

    private static Date format(String date, String dateFormatPattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormatPattern);
        try {
            return sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 格式化日期 到秒
     */
    public static Date formatDateTime(String date) {
        return getDate(date);
    }

    private static Date getDate(String date) {
        return format(date, DATE_TIME_FORMAT_PATTERN);
    }

    /**
     *  给日期加n天
     */
    public static Date addDay(Date dDate, int days) {
        return addDate(dDate, days, 5);
    }
    /**
     *  给日期加n月
     */
    public static Date addMonth(Date dDate, int months) {
        return addDate(dDate, months, 2);
    }
    /**
     *  给日期加n年
     */
    public static  Date addYear(Date dDate, int years) {
        return addDate(dDate, years, 1);
    }

    private static Date addDate(Date dDate, int amount, int field) {
        GregorianCalendar cl = new GregorianCalendar();
        cl.setTime(dDate);
        cl.add(field, amount);
        return cl.getTime();
    }

    /**
     * 当前日期下n天
     */
    public static Date nextDay(Date date, int day) {
        Calendar cal = Calendar.getInstance();
        if (date != null) {
            cal.setTime(date);
        }

        cal.add(Calendar.DAY_OF_YEAR, day);
        return cal.getTime();
    }
}
